﻿using CommunityToolkit.Mvvm.Input;
using DeviceController.Wpf.SaveInfos;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace DeviceController.Wpf.ViewModels.UserManageViewModels
{
    public class UserRegisterViewModel
    {
        private readonly UserManager _userManager;

        public UserRegisterViewModel(UserManager userManager)
        {
            _userManager = userManager;
            RegisterCommand = new RelayCommand<PasswordBox>(OnRegister);
        }

        private void OnRegister(PasswordBox? password)
        {
            this.NewUserPassword = password.Password;
            if (string.IsNullOrWhiteSpace(NewUserNumber) || NewUserNumber.Length != 8)
            {
                MessageBox.Show("员工工号长度必须为8个字符串长度！");
                return;
            }

            if (string.IsNullOrWhiteSpace(NewUserPassword) || NewUserPassword.Length < 6)
            {
                MessageBox.Show("员工卡 ID 长度必须大于6个字符串长度！");
                return;
            }

            try
            {
                _userManager.Register(new UserSaveInfo()
                {
                    Number = NewUserNumber,
                    Password = NewUserPassword,
                    Permession = Permession
                });

                MessageBox.Show("注册新用户成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 员工工号
        /// </summary>
        public string? NewUserNumber { get; set; }

        /// <summary>
        /// 员工刷卡 id
        /// </summary>
        public string? NewUserPassword { get; set; }

        /// <summary>
        /// 用户权限。
        /// </summary>
        public UserPermessions Permession { get; set; }

        public ICommand RegisterCommand { get; set; }
    }
}
